<template>
  <div>
    <router-link to="/" tag="div" class="header-abs" v-show="showabs">
      <div class="iconfont header-abs-back">&#xe624;</div>
    </router-link>

    <div class="header-fixed" v-show="!showabs" :style="opacityStyle">
      <p>景点选择</p>
      <router-link to="/" tag="span">
        <div class="iconfont header-fixed-back">&#xe624;</div>
      </router-link>
    </div>
  </div>
</template>

<script>
export default {
  name: "DeatailHeader",
  data() {
    return {
      showabs: true,
      opacityStyle:{
          opacity:0
      }
    };
  },
  methods: {
    handleScroll() {
        const top = document.documentElement.scrollTop
        if(top > 60){
            this.showabs = false
            let opacity = top/140
            opacity = opacity > 1 ? 1 : opacity
            this.opacityStyle = {
                opacity
            }
        }
        else
        {
            this.showabs = true 
        }
    }
  },
  //activated生命周期钩子函数是在App.vue中写入了keep-alive标签就会多出一个此生命周期函数的钩子。
  activated() {
    //写了window对全局绑定了scroll事件 所以要解绑
    window.addEventListener("scroll", this.handleScroll);
  },
  deactivated(){
    //对全局的scroll的事件进行解绑，防止其他页面也会进行scroll事件，保证scroll事件只会在此页面进行
    window.removeEventListener("scroll", this.handleScroll);
  }
};
</script>

<style lang="stylus" scoped>
@import '~styles/varibles.styl';

.header-abs {
  position: absolute;
  left: 0.2rem;
  top: 0.2rem;
  width: 0.8rem;
  height: 0.8rem;
  border-radius: 0.4rem;
  background: #000;

  .header-abs-back {
    color: #fff;
    text-align: center;
    line-height: 0.8rem;
    font-size: 0.4rem;
  }
}

.header-fixed {
  position: fixed;
  z-index 3
  top: 0;
  left: 0;
  right: 0;
  overflow: hidden;
  height: 0.86rem;
  line-height: 0.86rem;
  background: $bgColor;
  text-align: center;
  color: #fff;
  font-size: 0.32rem;

  .header-fixed-back {
    width: 0.64rem;
    text-align: center;
    position: absolute;
    top: 0;
    left: 0;
    font-size: 0.4rem;
  }
}
</style>
